<?php
/**
 * Created by PhpStorm.
 * User: 69571
 * Date: 2019/8/8
 * Time: 19:13
 */

namespace app\controllers;

use app\models\Vip;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Upload;

class VipController extends Controller
{

    /*
     *取消yii 框架自带的头和尾样式
    */
    public $enableCsrfValidation = false;

    /**
     * 添加会员
     */
    public function actionVipadd()
    {
        $request = Yii::$app->request;
        $formData = $request->post();
        $result = Vip::vipadd($formData);
        echo json_encode($result);
    }

    /**
     * 删除的会员(假)
     */
    public function actionVipdel()
    {
        $request = Yii::$app->request;
        $id = $request->post('id');
        $result = Yii::$app->db->createCommand()->update('vip', ['is_vip' => 0], "vip_id = $id")->execute();
        return $result ? 1 : 0;
    }

    /**
     * 逻辑删除，删除后放入到删除的会员列表中
     */
    public function actionVipdelall()
    {
        $request = Yii::$app->request;
        $ids = rtrim(Yii::$app->request->post('str'), ',');

        $result = Yii::$app->db->createCommand("UPDATE vip SET is_vip = 0 WHERE vip_id in ($ids) ")->execute();
        return $result ? 1 : 0;
    }

    /**
     * 还原会员
     * @return int
     * @throws \yii\db\Exception
     * 0成功 1失败
     */
    public function actionReduction(){
        $request = Yii::$app->request;
        $id = $request->post('id');
        $result = Yii::$app->db->createCommand()->update('vip', ['is_vip' => 1], "vip_id = $id")->execute();
        return $result?0:1;
    }


//    /**
//     * 删除的会员
//     */
//    public function actionVipdel()
//    {
//        $request = Yii::$app->request;
//        $id = $request->post('id');
//        $result = Yii::$app->db->createCommand()->delete('vip', "vip_id=$id")->execute();
//        return $result ? 1 : 0;
//    }
//
//    /**
//     * 删除的会员（批删）
//     */
//    public function actionVipdelall()
//    {
//        $request = Yii::$app->request;
//        $id = rtrim(Yii::$app->request->post('str'), ',');
//        $result = Yii::$app->db->createCommand("DELETE FROM `vip` WHERE vip_id in ($id)")
//            ->execute();
//        return $result ? 1 : 0;
//    }
    /**
     * 启动
     */
    public function actionOpen()
    {
        $request = Yii::$app->request;
        $id = $request->post('id');
        $state = $request->post('state');
        $result = Yii::$app->db->createCommand()->update('vip', ['vip_state' => $state], "vip_id = $id")->execute();
        return $result?0:1;
    }
    /**
     * 停用
     */
    public function actionStop()
    {
        $request = Yii::$app->request;
        $id = $request->post('id');
        $result = Yii::$app->db->createCommand()->update('vip', ['vip_state' => 1], "vip_id = $id")->execute();
        return $result?0:1;
    }
}